package cn.mumzone.pos.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.github.jdsjlzx.recyclerview.LRecyclerView;
import com.ruffian.library.widget.RTextView;

import java.util.Calendar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.mumzone.basic.base.BaseFragment;
import cn.mumzone.basic.net.response.PageListResponse;
import cn.mumzone.basic.util.AppDateUtil;
import cn.mumzone.basic.util.BigDecimalUtil;
import cn.mumzone.basic.util.IsFastClickUtil;
import cn.mumzone.basic.util.ListUtil;
import cn.mumzone.basic.util.PosToast;
import cn.mumzone.basic.util.RxBus;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.R;
import cn.mumzone.pos.event.MoreViewChangeEvent;
import cn.mumzone.pos.model.OffWorkEntity;
import cn.mumzone.pos.model.OffWorkTicketEntity;
import cn.mumzone.pos.presenter.OffWorkRecordPresenter;
import cn.mumzone.pos.presenter.contract.OffWorkRecordContract;
import cn.mumzone.pos.ui.adapter.ADA_OffWork;
import cn.mumzone.pos.ui.widgets.util.StringSpanUtils;
import cn.mumzone.pos.util.print.PrinterService;

/**
 * Created By Dlx
 * on 2019/7/12
 */
public class FRA_OffWorkRecord extends BaseFragment<OffWorkRecordPresenter> implements OffWorkRecordContract.View, ADA_OffWork.OnItemClickListener {
    @BindView(R.id.iv_back)
    AppCompatImageView ivBack;
    @BindView(R.id.rv_cashier)
    RecyclerView rvCashier;
    @BindView(R.id.tv_staff_name)
    TextView tvStaffName;
    @BindView(R.id.tv_staff_duty)
    TextView tvStaffDuty;
    @BindView(R.id.tv_work_duration)
    TextView tvWorkDuration;
    @BindView(R.id.tv_amount_receivable)
    TextView tvAmountReceivable;
    @BindView(R.id.tv_real_amount)
    TextView tvRealAmount;
    @BindView(R.id.tv_check_amount)
    TextView tvCheckAmount;
    @BindView(R.id.tv_money_amount)
    TextView tvMoneyAmount;
    @BindView(R.id.tv_money_check)
    TextView tvMoneyCheck;
    @BindView(R.id.tv_money_difference)
    TextView tvMoneyDifference;
    @BindView(R.id.tv_bank_card_amount)
    TextView tvBankCardAmount;
    @BindView(R.id.tv_alipay_amount)
    TextView tvAlipayAmount;
    @BindView(R.id.tv_wechat_amount)
    TextView tvWechatAmount;
    @BindView(R.id.tv_member_amount)
    TextView tvMemberAmount;
    @BindView(R.id.tv_time_card)
    TextView tvTimeCard;
    @BindView(R.id.tv_integral_title)
    TextView tvIntegralTitle;
    @BindView(R.id.tv_integral)
    TextView tvIntegral;
    @BindView(R.id.ll_detail_group)
    LinearLayout llDetailGroup;
    @BindView(R.id.tv_print_btn)
    RTextView tvPrintBtn;

    private String startTime;
    private String endTime;

    private ADA_OffWork mAdapter;
    private OffWorkEntity mSelectItem;

    private int pageNum = 1;

    @Override
    protected void onUserVisible() {
        mPresenter.loadOffWorkRecord(startTime, endTime, "", pageNum + "");
    }

    @Override
    protected void initEventAndData() {
        mAdapter = new ADA_OffWork(mContext);
        mAdapter.setOnItemClickListener(this);
        rvCashier.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        rvCashier.setAdapter(mAdapter);

        // 获取交班列表
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());

        // 开始时间
        startTime = AppDateUtil.getTimeStamp(calendar.getTime(), AppDateUtil.YYYY_MM_DD);
        // 结束时间 - 默认只查当天
        calendar.add(Calendar.DAY_OF_MONTH, 1);
        endTime = AppDateUtil.getTimeStamp(calendar.getTime(), AppDateUtil.YYYY_MM_DD);

        mPresenter.loadOffWorkRecord(startTime, endTime, "", pageNum + "");
    }

    @Override
    protected int getLayout() {
        return R.layout.fra_off_work_record;
    }

    @Override
    public void showOffWorkRecord(PageListResponse<OffWorkEntity> list) {
//        if (ListUtil.isListEmpty(list.getList())) {
//            toggleShowEmpty(true, R.drawable.ic_search_empty, "这里还没有内容哦！");
//            return;
//        }

//        toggleShowEmpty(false, -1, "");

        mAdapter.clearCheck();
        rvCashier.setAdapter(mAdapter);
        mAdapter.update(list.getList(), true);
    }

    @Override
    public void doPrintShiftTicket(OffWorkTicketEntity entity) {
        PrinterService.doPrintOffWordTicket(mContext, entity);
    }

    @OnClick({R.id.iv_back, R.id.tv_print_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                RxBus.getDefault().post(new MoreViewChangeEvent(MoreViewChangeEvent.MORE_VIE_HOME));
                break;
            case R.id.tv_print_btn:
                if (!IsFastClickUtil.isFastClick(view)) {
                    mPresenter.loadShiftDetail(mSelectItem.getId());
                }
                break;
        }
    }

    @Override
    public void onItemSelected(OffWorkEntity offWorkEntity) {

        mSelectItem = offWorkEntity;

        // 交班人
        if (StringUtils.isTrue(offWorkEntity.getIsOnWork())) {
            tvStaffName.setText(StringSpanUtils.init().addStr(offWorkEntity.getUserName()).addStr("    ").addRoundRectLabelWithRadius(" 在班 ", tvStaffName,
                    mContext.getResources().getColor(R.color.white_color),
                    mContext.getResources().getColor(R.color.order_status_reserve),
                    1, 1, 1, 0, true).build());
            tvPrintBtn.setVisibility(View.GONE);
        } else {
            tvPrintBtn.setVisibility(View.VISIBLE);
            tvStaffName.setText(offWorkEntity.getUserName());
        }
        // 在班时间
//        String startTime = AppDateUtil.getTimeStamp(Long.parseLong(offWorkEntity.getStartTime()), AppDateUtil.MM_DD_HH_MM);
//        String endTime = AppDateUtil.getTimeStamp(Long.parseLong(offWorkEntity.getEndTime()), AppDateUtil.MM_DD_HH_MM);
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append(offWorkEntity.getStartTime())
                .append(" - ")
                .append(offWorkEntity.getEndTime());
        tvWorkDuration.setText(stringBuffer.toString());

        // 应收金额
        tvAmountReceivable.setText("￥" + StringUtils.getFormattedMoney(offWorkEntity.getReceivableAmount()));

        // 实收金额
        tvRealAmount.setText("￥" + StringUtils.getFormattedMoney(offWorkEntity.getActualAmount()));

        // 实盘金额
        tvCheckAmount.setText("￥" + StringUtils.getFormattedMoney(offWorkEntity.getQuotationAmount()));

        // 现金实收
        tvMoneyAmount.setText("￥" + StringUtils.getFormattedMoney(offWorkEntity.getCashAmount()));
        // 现金实盘
        tvMoneyCheck.setText("￥" + StringUtils.getFormattedMoney(offWorkEntity.getCashFirm()));
        // 现金溢缺
        tvMoneyDifference.setText("￥" + StringUtils.getFormattedMoney(offWorkEntity.getOverAndShort()));

        // 银行卡
        tvBankCardAmount.setText("￥" + StringUtils.getFormattedMoney(offWorkEntity.getCardAmount()));
//        tvBankCollectedFinally.setText("");

        // 支付宝
        tvAlipayAmount.setText("￥" + StringUtils.getFormattedMoney(offWorkEntity.getAliAmount()));
//        tvAliCollectedFinally.setText("");

        // 微信
        tvWechatAmount.setText("￥" + StringUtils.getFormattedMoney(offWorkEntity.getWechatAmount()));
//        tvWxCollectedFinally.setText("");


        //会员余额支付
        tvMemberAmount.setText("￥" + StringUtils.getFormattedMoney(offWorkEntity.getMemberAmount()));
//        tvBalanceCollectedFinally.setText("");

        //计次卡刷卡支付
        tvTimeCard.setText("￥" + StringUtils.getFormattedMoney(offWorkEntity.getTimesCardAmount()));
//        tvCardCollectedFinally.setText("");

        //积分抵现
        tvIntegralTitle.setText(String.format("%s积分抵现", StringUtils.getWholeNumber(BigDecimalUtil.getNumber(offWorkEntity.getIntegralMoney()))));
        tvIntegral.setText(StringUtils.getFormattedMoneyWithUnit(offWorkEntity.getIntegralAmount()));
    }
}
